<template>
    <div class="matchground">
        <div class="row">
            <div class="col-6">
                <div class="user-photo">
                    <img :src="$store.state.user.photo">
                </div>
                <div class="user-username">
                    {{ $store.state.user.username }}
                </div>
            </div>
            <div class="col-6">
                <div class="user-photo">
                    <img :src="$store.state.pk.opponent_photo">
                </div>
                <div class="user-username">
                    {{ $store.state.pk.opponent_username }}
                </div>
            </div>
            <div class="col-12">
                <div class="user-select-bot">
                    <select v-model="select_bot" class="form-select" aria-label="Default select example">
                        <option value="-1" selected>Player**</option>

                        <option v-for="bot in bots" :key="bot.id" :value="bot.id">{{ bot.title }}</option>
                    </select>
                </div>
            </div>
            <div class="col-12"> <button type="button" class="btn btn-success btn-large" @click="click_match_button">{{
                match_button
            }}</button></div>


        </div>
    </div>
</template>

<script>
import { ref } from 'vue';
import { useStore } from 'vuex';
import $ from 'jquery';

export default {
    setup() {
        let match_button = ref("开始匹配");
        const store = useStore();
        let bots = ref([]);
        let select_bot = ref('-1');

        const click_match_button = () => {
            if (match_button.value === "开始匹配") {
                match_button.value = "取消匹配";
                store.state.pk.socket.send(JSON.stringify({
                    event: "start matching",
                    bot_id: select_bot.value
                }));
            } else {
                match_button.value = "开始匹配";
                store.state.pk.socket.send(JSON.stringify({
                    event: "stop matching",
                }));
            }
        }

        const refresh_bots = () => {
            $.ajax({
                url: "http://123.60.185.149:8100/user/bot/getlist",
                type: "post",
                headers: {
                    Authorization: "Bearer " + store.state.user.token,
                },
                success(resp) {
                    bots.value = resp;
                }
            })
        }
        refresh_bots();

        return {
            match_button,
            click_match_button,
            bots,
            select_bot
        }
    }
}
</script>

<style scoped>
div.matchground {
    width: 60vw;
    height: 70vh;
    margin: 6% auto;
    background-color: rgba(10, 10, 10, 0.8);
}

div.user-photo {
    margin-top: 12vh;
    text-align: center;
}

div.user-photo>img {
    border-radius: 50%;
    width: 200px;
    height: 200px;
}

div.user-username {
    text-align: center;
    font-size: 200%;
    font-weight: 400;
    margin-top: 2vh;
    color: white;
}

div.col-12 {
    text-align: center;
    margin-top: 2%;
}

div.col-12>div.user-select-bot {
    width: 30%;
    margin: 0 auto;
}


div.col-12>button {
    font-size: xx-large;
}
</style>